var vm = new Vue({
    el: '#app',
    data: {
        host: host,

        mobile: '',
        username: '',
        sms_code: '',
        password: '',
        allow: false,

        userinfo: '',
        pasword: '',
        remlogin: false,

    },
    methods: {
        // 获取url路径参数
        get_query_string: function(name){
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
        register: function () {
            // 注册
            axios.post(this.host + '/users/', {
                username: this.username,
                password: this.password,
                mobile: this.mobile,
                sms_code: this.sms_code,
                allow: this.allow.toString()  // 转换为1/0
            }, {
                responseType: 'json'
            })
                .then(response => {
                    // 记录用户的登录状态
                    sessionStorage.clear(); // 清除所有保存的值
                    localStorage.clear();
                    localStorage.token = response.data.token; // 重新赋值
                    localStorage.username = response.data.username;
                    localStorage.user_id = response.data.id;
                    location.href = 'headline-logined.html';
                })
                .catch(error => {
                    if (error.response.status == 400) {
                        alert('数据有误')
                    } else {
                        alert(error.response.data)
                    }
                })
        },
        // 登录
        login: function () {
            axios.post(this.host + '/authorizations/', {
                username: this.userinfo,
                password: this.pasword,
            }, {
                responseType: 'json',
                withCredentials: true  // 允许前端携带cookie
            })
                .then(response => {
                    // alert(1)
                    // 使用浏览器本地存储保存token
                    if (this.remlogin) {
                        // 记住登录
                        sessionStorage.clear();
                        localStorage.token = response.data.token;
                        localStorage.user_id = response.data.user_id;
                        localStorage.username = response.data.username;
                    } else {
                        // 未记住登录
                        localStorage.clear();
                        sessionStorage.token = response.data.token;
                        sessionStorage.user_id = response.data.user_id;
                        sessionStorage.username = response.data.username;
                    }

                    // 跳转页面
                    var return_url = this.get_query_string('next');
                    if (!return_url) {
                        return_url = 'headline-logined.html';
                    }
                    location.href = return_url;
                })
                .catch(error => {
                        alert('用户名或密码错误');
                })
        },


    }
})